package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.entity.Category;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.service.CategoryBrandService;
import com.leyou.item.service.CategoryService;
import com.leyou.item.dto.CategoryDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>implements CategoryService {
    @Autowired
    private CategoryBrandService categoryBrandService;
    @Override
    public List<CategoryDTO> queryCategoryByPid(Long pid) {

        return CategoryDTO.convertEntityList(this.query().eq("parent_id",pid).list());
    }

    @Override
    public List<CategoryDTO> queryCategoryByBrand(Long bid) {
        //查询获取到品牌对应的分类的id的集合
        List<Long>cids=this.categoryBrandService
                .query()
                .eq("brand_id",bid)
                .list()
                .stream()
                .map(CategoryBrand::getCategoryId)
                .collect(Collectors.toList());
        return CategoryDTO.convertEntityList(this.listByIds(cids));
    }


}
